package com.xia.service.impl;

import com.xia.dao.article.IArticleDao;
import com.xia.dao.article.ICommentDao;
import com.xia.domian.Article;
import com.xia.domian.Comment;
import com.xia.domian.ReplyComment;
import com.xia.service.ICommentService;
import com.xia.util.exception.CodeEnum;
import com.xia.util.exception.ServiceException;
import com.xia.util.websocket.Websocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 评论业务实现类
 */
@Service
public class ICommentServiceImpl implements ICommentService {
    private ICommentDao commentDao;
    private IArticleDao articleDao;
    @Resource
    private String getDate;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    public void setArticleDao(IArticleDao articleDao) {
        this.articleDao = articleDao;
    }
    @Autowired
    public void setCommunityDao(ICommentDao commentDao) {
        this.commentDao = commentDao;
    }

    @Override
    public List<Comment> selectReplyOneselfCommentServer(Integer usersId){
        //定义查询redis新回复key
        String infoCommentKey="informUsersCommentId_"+usersId;
        //redis中是否有新回复key
        Boolean state=redisTemplate.hasKey(infoCommentKey);
        //定义回复key
        String commentKey="replyUsersCommentId_"+usersId;
        //如果redis中有该用户的新回复key进入
        if (state){
            //根据新回复key查询数据
            List<Comment> list=selectReplyOneselfCommentServerUtil(infoCommentKey);
            for (Comment informComment : list) {
                //将新回复的数据加入回复数据
                redisTemplate.opsForSet().add(commentKey, informComment);
            }
            //添加后根据新回复key删除该数据
            redisTemplate.delete(infoCommentKey);
            return selectReplyOneselfCommentServerUtil(commentKey);
        }
        return selectReplyOneselfCommentServerUtil(commentKey);
    }

    public List<Comment> selectReplyOneselfCommentServerUtil(String infoCommentKey){
        Set<Object> objectSet=redisTemplate.opsForSet().members(infoCommentKey);
        List<Comment> list=new ArrayList<>();
        for (Object o : objectSet) {
            Comment comment=(Comment) o;
            list.add(comment);
        }
        return list;
    }

    @Override
    public Map<String, Object> userComment(Comment comment) throws IOException {
        comment.setCommentCreatingTime(getDate);
        int insertComment=commentDao.insertComment(comment);
        if (insertComment == 0){
            throw new ServiceException(CodeEnum.CODE_400,"错误的评论提交");
        }
        //判断是否为二级评论
        if(comment.getCommentUsers() == 0){
            return selectArticleComment(comment.getCommentArticle(),1);
        }
        //判断是否为视频评论
        if(comment.getCommentType() == 2){
            return selectArticleComment(comment.getCommentArticle(),2);
        }
        List<Article> articleList=articleDao.selectArticleOnId(0,comment.getCommentArticle(),0,0);
        String usersIdKey="informUsersCommentId_"+articleList.get(0).getUsers().getUsersId();
        Comment multipleInformComment=commentDao.selectInformComment(comment.getCommentId());
        redisTemplate.opsForSet().add(usersIdKey,multipleInformComment);
        String usersId=Integer.toString(multipleInformComment.getArticle().getArticleAuthor());
        Websocket.sendMessage(usersId,"有一条新评论");
        return selectArticleComment(comment.getCommentArticle(),1);
    }

    @Override
    public Map<String, Object> selectArticleComment(Integer articleId,Integer commentType) {
        Map<String, Object> map=new HashMap<>();
        int commentAndReplySize;
        List<Comment> commentList=commentDao.selectComment(articleId,0,commentType);
        commentAndReplySize=commentList.size();
        if (commentList.size() != 0){
            for (Comment comment : commentList) {
                List<ReplyComment> replyList = commentDao.selectReply(comment.getCommentId(), 0);
                if (replyList.size() != 0) {
                    commentAndReplySize += replyList.size();
                    comment.setReplyCommentList(replyList);
                    continue;
                }
                comment.setReplyCommentList(null);
            }
        }
        map.put("commentAndReplyContent",commentList);
        map.put("commentListSize",commentAndReplySize);
        return map;
    }

}
